package com.jingdianjichi.auth.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.convert.AuthUserDTOConverter;
import com.jingdianjichi.auth.domain.entity.AuthUserBO;
import com.jingdianjichi.auth.domain.service.AuthUserDomainService;
import com.jingdianjichi.auth.dto.AuthUserDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/user/")
public class UserController {

    @Resource
    AuthUserDomainService authUserDomainService;

    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
    @RequestMapping("doLogin")
    public Result<SaTokenInfo> doLogin(@RequestParam("validCode")String vaildCode) {

        try {
            Preconditions.checkArgument(!StringUtils.isEmpty(vaildCode),"验证码不能为空");
            SaTokenInfo saTokenInfo= authUserDomainService.doLogin(vaildCode);
            return Result.ok(saTokenInfo);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.fail("用户登录失败");
        }

    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    @RequestMapping("register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDto) {
        try {
            if(log.isInfoEnabled()){
                log.info("add subjectdto category", JSON.toJSONString(authUserDto));
            }
            check(authUserDto);
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDtoToBoAuthUser(authUserDto);
            authUserDomainService.register(authUserBO);
            return Result.ok(true);
        } catch (Exception e) {
            return Result.fail("注册用户失败");
        }
    }

    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDto) {
        try {
            if(log.isInfoEnabled()){
                log.info("update authUserDto category", JSON.toJSONString(authUserDto));
            }
            check(authUserDto);

            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDtoToBoAuthUser(authUserDto);

            return Result.ok(authUserDomainService.update(authUserBO));
        } catch (Exception e) {
            return Result.fail("更新用户失败");
        }
    }
    public void check(@RequestBody AuthUserDTO authUserDto){
        Preconditions.checkArgument(!StringUtils.isBlank(authUserDto.getUserName()),"用户名不能为空");
        Preconditions.checkArgument(!StringUtils.isBlank(authUserDto.getEmail()),"邮件不能为空");
        Preconditions.checkArgument(!StringUtils.isBlank(authUserDto.getPassword()),"密码不能为空");
    }

    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDto) {
        try {
            if(log.isInfoEnabled()){
                log.info("update authUserDto category", JSON.toJSONString(authUserDto));
            }
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDtoToBoAuthUser(authUserDto);

            return Result.ok(authUserDomainService.delete(authUserBO));
        } catch (Exception e) {
            return Result.fail("删除用户失败");
        }
    }
    @RequestMapping("changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.changeStatus.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getStatus(), "用户状态不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDtoToBoAuthUser(authUserDTO);
            return Result.ok(authUserDomainService.update(authUserBO));
        } catch (Exception e) {
            log.error("UserController.changeStatus.error:{}", e.getMessage(), e);
            return Result.fail("启用/禁用用户信息失败");
        }
    }

    @RequestMapping("getUserInfo")
    public Result<AuthUserDTO> getUserInfo(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.changeStatus.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()),"用户名不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDtoToBoAuthUser(authUserDTO);
            AuthUserBO authUserBO1 = authUserDomainService.getUserInfo(authUserBO);
            return Result.ok(AuthUserDTOConverter.INSTANCE.convertBoAuthUserToDto(authUserBO1));
        } catch (Exception e) {
            log.error("UserController.changeStatus.error:{}", e.getMessage(), e);
            return Result.fail("获取用户信息失败");
        }
    }
    @RequestMapping("logOut")
    public Result<Boolean> logOut(@RequestParam String userName) {

        try {
            log.info("UserController.logOut.dto:{}", userName);
            Preconditions.checkArgument(!StringUtils.isEmpty(userName),"用户名不能为空");
            StpUtil.logout(userName);
            return Result.ok();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.fail("用户退出失败");
        }

    }
    
}
